//
//  MPGLAddViewController.m
//  mingpianguanli
//
//  Created by ryou on 2021/6/23.
//
#define kMpglTitleArray  @[@"名字",@"公司",@"职位",@"选择联系方式类型",@"联系方式",@"个人简介"]
#import "MPGLAddViewController.h"

@interface MPGLAddViewController ()<UITableViewDelegate,UITableViewDataSource,BUNativeExpressRewardedVideoAdDelegate,UITextFieldDelegate>
@property(nonatomic,strong)UITableView *mpglTableView;
@property(nonatomic,strong)UITextField *mpglTextField1;
@property(nonatomic,strong)UITextField *mpglTextField2;
@property(nonatomic,strong)UITextField *mpglTextField3;
@property(nonatomic,strong)UITextField *mpglTextField4;
@property(nonatomic,strong)UITextField *mpglTextField5;
@property(nonatomic,strong)UITextField *mpglTextField6;
@end

@implementation MPGLAddViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview: self.mpglTableView];
    self.mpglTableView.tableFooterView=[[UIView alloc]init];
    // Do any additional setup after loading the view.
    UIBarButtonItem *saveBtn=[[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(saveBtnClick)];
    
    self.navigationItem.rightBarButtonItem=saveBtn;

    [self mingpianguanliloadBannerWithSlotID];
    [self mingpianjiloadRewardVideoAd];
    // Do any additional setup after loading the view.
}
-(void)saveBtnClick{
    if (_mpglTextField1.text.length==0||
        _mpglTextField2.text.length==0||
        _mpglTextField3.text.length==0||
        _mpglTextField4.text.length==0||
        _mpglTextField5.text.length==0||
        _mpglTextField6.text.length==0) {

        UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"请输入完整" message:@"" preferredStyle:UIAlertControllerStyleAlert];
 
        UIAlertAction *action=[UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
             }];
            [alert addAction:action];
         [self presentViewController:alert animated:YES completion:nil];
        
        return;
    }
    NSUserDefaults *mpglUser=[NSUserDefaults standardUserDefaults];
    NSArray *mpglArray=[mpglUser valueForKey:@"mpgluserdata"];
    NSMutableArray *narray = [[NSMutableArray alloc]initWithArray:mpglArray];
    
    NSDictionary *dict=@{@"mingzi":_mpglTextField1.text,
                        @"gongsi":_mpglTextField2.text,
                        @"zhiwei":_mpglTextField3.text,
                        @"lianxifangshileixing":_mpglTextField4.text,
                        @"lianxifangshi":_mpglTextField5.text,
                        @"gerenjianjie":_mpglTextField6.text};
    [narray addObject:dict];
    [mpglUser setValue:narray forKey:@"mpgluserdata"];
    [self.navigationController popViewControllerAnimated:YES];
    
    
}
- (UITableView *)mpglTableView{
    if (!_mpglTableView) {
        _mpglTableView=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _mpglTableView.delegate=self;
        _mpglTableView.dataSource=self;
    }
    return _mpglTableView;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    UITableViewCell *mpglCrll=[tableView cellForRowAtIndexPath:indexPath];
    if (!mpglCrll) {
        mpglCrll=[[UITableViewCell alloc]init];
    }
    mpglCrll.selectionStyle=UITableViewCellSelectionStyleNone;
    mpglCrll.textLabel.text = kMpglTitleArray[indexPath.row];
    
    UITextField *textField=[[UITextField alloc]initWithFrame:CGRectMake(100, 0, kmpglWidth - 130, 80)];
    textField.textAlignment = NSTextAlignmentRight;
    textField.placeholder = @"请输入";
    textField.delegate=self;
    [mpglCrll.contentView addSubview:textField];
    
    if (indexPath.row == 3) {
    }
    switch (indexPath.row) {
        case 0:
            textField.text = _mpglTextField1.text;
            _mpglTextField1 = textField;
            break;
        case 1:
            textField.text = _mpglTextField2.text;
            _mpglTextField2 = textField;
            break;
        case 2:
            textField.text = _mpglTextField3.text;
            _mpglTextField3 = textField;
            break;
        case 3:
            textField.userInteractionEnabled=NO;
            mpglCrll.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            textField.text = _mpglTextField4.text?_mpglTextField4.text:@"请选择";
            _mpglTextField4 = textField;
            break;
        case 4:
            textField.text = _mpglTextField5.text;
            _mpglTextField5 = textField;
            break;
        case 5:
            textField.text = _mpglTextField6.text;
            _mpglTextField6 = textField;
            break;
        default:
            break;
    }
    return mpglCrll;
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    [self mingpianjishowRewardVideoAd];
    return YES;
}
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return kMpglTitleArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 3) {
        UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"请选择类型" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
        NSArray *array= @[@"电话",@"传真",@"QQ",@"邮箱",@"微信"];
        
        for (NSString *title in array) {
            
            UIAlertAction *action=[UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                _mpglTextField4.text = action.title;
                [self.mpglTableView reloadData];
            }];
            [alert addAction:action];
        }
        [self presentViewController:alert animated:YES completion:nil];
    }
}



- (void)mingpianjishowRewardVideoAd {
    if (self.mingpianRewardedVideoAd && self.mploadTag == 1 ) {
        NSUserDefaults *mpjuser=[NSUserDefaults standardUserDefaults];
        BOOL kan =  [mpjuser boolForKey:@"mingpianjikan"];
        if (kan) {
            UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"温馨提示" message:@"你正在使用高级功能，需要观看激励广告激活，是否观看？" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action1=[UIAlertAction actionWithTitle:@"观看广告" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                [self.mingpianRewardedVideoAd showAdFromRootViewController:self];
            }];
            UIAlertAction *action2=[UIAlertAction actionWithTitle:@"取消观看" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            }];
            [alert addAction:action1];
            [alert addAction:action2];
            [self presentViewController:alert animated:YES completion:nil];
        }else{
            [self.mingpianRewardedVideoAd showAdFromRootViewController:self];
        }
    }else if(self.mploadTag == 2){
        [self showviewController];
    }
}


- (void)mingpianjiloadRewardVideoAd{
    
    BURewardedVideoModel *model = [[BURewardedVideoModel alloc] init];
    self.mingpianRewardedVideoAd = [[BUNativeExpressRewardedVideoAd alloc] initWithSlotID:kjilikey rewardedVideoModel:model];
    self.mingpianRewardedVideoAd.delegate = self;
    [self.mingpianRewardedVideoAd loadAdData];
    
}
-(void)showviewController{
    self.mploadTag =0;
    [self mingpianjiloadRewardVideoAd];
    
  
}

#pragma mark - BUNativeExpressRewardedVideoAdDelegate

- (void)nativeExpressRewardedVideoAd:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd didFailWithError:(NSError *_Nullable)error {
    self.mploadTag = 2;
}


- (void)nativeExpressRewardedVideoAdDidDownLoadVideo:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd {
    self.mploadTag = 1;
    
}

- (void)nativeExpressRewardedVideoAdDidPlayFinish:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd didFailWithError:(NSError *_Nullable)error {
    [self showviewController];
}
@end
